var e = getApp();

Page({
    data: {
        imagebase64: "",
        show: !1,
        height: 0,
        width: 0,
        bgColor: "",
        fontVaColorArray: [ "#ffffff", "#FF0000", "#438EDB", "#000000", "#FFFF00", "#00FF00", "#FF0000", "#00FFFF", "#FF00FF" ],
        fontColorArray: [ "白色", "红色", "蓝色", "黑色", "黄色", "绿色", "橙色", "靛色", "紫色" ],
        tempPath: "",
        ad: {}
    },
    bindFontCoPickerChange: function(e) {
        this.getRandomAd()
        var a = this;
        console.log(e), this.setData({
            bgColor: a.data.fontVaColorArray[e.detail.value]
        });
    },
    clearColor: function() {
        this.getRandomAd()
        this.setData({
            bgColor: ""
        });
    },
    onShow(){
        this.getRandomAd()
        getApp().chaping()
    },
    onLoad: function(a) {
        getApp().getRandomAd(true,this)
        this.getRandomAd(true)
    },
    upload: function() {
        var e = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "album" ],
            success: async function(a) {
                var t = a.tempFilePaths;
                let imgData = await getApp().checkImg(t[0],e)
                console.log(imgData)
                if(!imgData||imgData.result.errCode!==0){
                    wx.showModal({
                    confirmText: '确定',
                    content: '图片内容不合法，请重新选择',
                    showCancel: false
                    })
                    return
                }
                e.uploadimage(e, t[0]);
            }
        });
    },
    uploadimage: function(e, a) {
        wx.showLoading({
            title: "抠图中"
        }), wx.uploadFile({
            url: "https://files.ishare20.net/imagematting/api.php",
            filePath: a,
            name: "file",
            success: function(a) {
                wx.hideLoading({
                    success: function(e) {}
                }), e.setData({
                    imagebase64: JSON.parse(a.data),
                    show: !0
                });
                var t = e.data.imagebase64, o = wx.getFileSystemManager(), n = wx.env.USER_DATA_PATH + "/pic" + Date.parse(new Date()) + ".png";
                o.writeFile({
                    filePath: n,
                    data: t,
                    encoding: "base64",
                    success: function(a) {
                        wx.getImageInfo({
                            src: n,
                            success: function(a) {
                                e.setData({
                                    height: a.height,
                                    width: a.width,
                                    tempPath: n
                                });
                            }
                        });
                    },
                    fail: function(e) {
                        console.log(e), wx.hideLoading({
                            complete: function(e) {}
                        }), wx.showToast({
                            title: "服务器出错，请稍后重试",
                            icon: "none"
                        });
                    }
                });
            }
        });
    },
    doImgSecCheck: function(e) {
        wx.serviceMarket.invokeService({
            service: "wxee446d7507c68b11",
            api: "imgSecCheck",
            data: {
                Action: "ImageModeration",
                Scenes: [ "PORN", "POLITICS", "TERRORISM" ],
                ImageUrl: "",
                ImageBase64: e,
                Config: "",
                Extra: ""
            }
        }).then(function(e) {
            return console.log(JSON.stringify(e)), "BLOCK" != e.data.Response.Suggestion;
        });
    },
    save: function() {
        this.getRandomAd()
        wx.showLoading({
            title: "保存中"
        });
        var e = this, a = wx.createCanvasContext("mycanvas");
        e.data.bgColor && (a.setFillStyle(e.data.bgColor), a.fillRect(0, 0, e.data.width, e.data.height)), 
        a.drawImage(e.data.tempPath, 0, 0, e.data.width, e.data.height), a.draw(), setTimeout(function() {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                canvasId: "mycanvas",
                success: function(e) {
                    wx.saveImageToPhotosAlbum({
                        filePath: e.tempFilePath,
                        success: function(e) {
                            wx.hideLoading(), wx.showToast({
                                title: "保存成功"
                            });
                        },
                        fail: function(e) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '需要您授权保存相册',
                                showCancel: false,
                                success: modalSuccess => {
                                  wx.openSetting({
                                    success(settingdata) {
                                      console.log("settingdata", settingdata)
                                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                        wx.showModal({
                                          title: '提示',
                                          content: '获取权限成功,再次点击即可保存。',
                                          showCancel: false,
                                        })
                                      } else {
                                        wx.showModal({
                                          title: '提示',
                                          content: '获取权限失败，将无法保存到相册哦~',
                                          showCancel: false,
                                        })
                                      }
                                    },
                                    fail(failData) {
                                      console.log("failData", failData)
                                    },
                                    complete(finishData) {
                                      console.log("finishData", finishData)
                                    }
                                  })
                                }
                            })
                        }
                    }), console.log(e);
                },
                fail: function(e) {
                    wx.hideLoading(), console.log(e), wx.showToast({
                        title: "保存失败"
                    });
                }
            });
        }, 300);
    },
    onReady: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: "AI人像抠图-图片编辑助手",
            path: "pages/imagematting/imagematting"
        };
    }
});